let app = getApp() 
import detailCanvas from '../../utils/sharekan.js';
Component({
    properties: {  
      orderid:String,
      activity_id:String,
      launch_id:Number 
      },
      data:{
        moneySign:'¥',
        siteDomain:app.globalData.siteDomain,
        clearTimer: false,
        saveText:'保存到相册，分享给好友'
      },
    lifetimes: {
      ready:function(){
        wx.getSystemInfo({
          success: (res)=>{
            this.setData({
              screen_width: res.windowWidth/375,
              screen_height: res.windowHeight
           })
          },
        });
      },
        attached: function () {
            this.setData({
              globalColor:app.globalData.globalColor
            })
            var that=this
            wx.request({
             url: app.globalData.siteroot,
              data: {
                do:'Bargain_getBargainUser',
                launch_id:that.data.orderid,
                activity_id:that.data.activity_id
             },
            success: function(res) {
              var obj = {
                goods_id:res.data.data.goods.id,
                sku_id:res.data.data.goods.sku_id,
                num:res.data.data.goods.num,
                template_id:1
               } 
               var product=[];
               product.push(obj);
               wx.setStorageSync('confirmgoods',JSON.stringify(product));

               that.setData({
                userId:wx.getStorageSync('user_id'),
                activityInfo:res.data.data.activity,
                productInfo:res.data.data.goods,
                targetTime:Date.parse(new Date())/1000+res.data.data.activity.end_time,
                processWidth:(Number(res.data.data.activity.high_price)-Number(res.data.data.activity.current_price))/Number(res.data.data.activity.high_price)* 100 + "%"
               })
            }
          })
          wx.request({
            url: app.globalData.siteroot,
             data: {
               do:'Bargain_getHelpPrice',
               launch_id:that.data.orderid,
               activity_id:that.data.activity_id,
               user_id:wx.getStorageSync('user_id')
            },
           success: function(res) {
             if(res.data.data.is_help==1){
              that.setData({
                visible:false,
                show:false
               })
             }else{
              that.setData({
                visible:true,
                show:true,
                current_price:res.data.data.current_price,
                cut_money:res.data.data.cut_money
               })
             }
           }
         })
       }
      },
    methods: {
      cancel:function(){
        this.setData({
          visible:false,
          show:false,
        })
      },
      nowbuy:function(){
        wx.redirectTo({
          url: '/yb_shopv2/native/all/index?type=confirm_order&orderid='+this.data.orderid+'&activity_id='+this.data.activity_id+'&page_type=bargain',
        })
      },
      sharehaibao:function(){
        wx.showLoading({
          title: '绘制中',
          mask: true,
        });
        let that = this;
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Qrcode_getQrCode',
            path:'yb_shopv2/native/all/index?type=good_detail&goods_id='+this.data.productInfo.id
          },
          success: function(res) {
               that.setData({
                codeurl:res.data.qrurl
               })
               var userinfo=wx.getStorageSync('userinfo');
               let title =that.data.productInfo.goods_name;
               let detailImg =that.data.productInfo.pic;
               let current_price =that.data.activityInfo.current_price;
               let codeImg =res.data.qrurl;
               let nickName = userinfo.nick_name
               let headUrl = app.globalData.siteDomain+'/addons/yb_shopv2_video/core/head_convert.php?url='+that.data.activityInfo.user_headimg;
               Promise.all([
                that.getCode(detailImg),
                that.getCode(codeImg),
                that.getCode(headUrl)
               ]).then(res=>{
                   let sWidth = that.data.screen_width;
                   let sHeight = that.data.screen_height;
                   let code = res[1].tempFilePath;
                   let img = res[0].tempFilePath;
                   let headimg = res[2].tempFilePath;
                   detailCanvas(that,sWidth,sHeight,headimg,img,code,title,current_price,nickName,function(res){
                   wx.hideLoading();
                   that.setData({
                     posters:res
                   });
                 });
               })
          }
        })
            this.setData({
              shareDialogVisible:false,
              shareDialogMask:false,
              shareDialogPosterVisible:true,
              shareDialogPosterMask:true
            })
        },
        saveShareImg: function () {
         let posters = this.data.posters;
         wx.saveImageToPhotosAlbum({
          filePath: posters,
          success: (res)=>{
            wx.showToast({
              title:'保存成功',
              icon:'success'
            })
            this.setData({
              posters:'',
              shareDialogPosterVisible:false,
              shareDialogPosterMask:false
            })
          },
          fail: (err)=>{
            wx.showToast({
              title: '请授权保存图片',
              icon: 'none',
            });
            this.setData({
              isShare:true
            })
          },
        })
          
        },
        getCode(img){
          return new Promise((resolve,reject)=>{
            wx.downloadFile({
              url: img,
              success: (res) => {
                resolve(res)
              }
            })
          })
        },
      callfriend:function(){
        this.setData({
          posterVisible:true,
          posterShow:true
        })
        this.sharehaibao();
      },
      goodsdetail:function(){
        wx.navigateTo({
          url: '/yb_shopv2/native/all/index?type=good_detail&goods_id='+this.data.productInfo.id,
        })
      },
      helpcan:function(){
        wx.navigateTo({
          url: '/yb_shopv2/native/all/index?type=bargain_record&orderid='+this.data.orderid+'&activity_id='+this.data.activity_id,
        })
      }
    }
});
